package com.android.nearshop.app.adapter.listadapter;

import java.util.List;

import com.android.nearshop.R;
import com.android.nearshop.app.activity.shop.ShopDetailActivity;
import com.android.nearshop.app.adapter.listadapter.CommonListAdapter.ViewHolder;
import com.android.nearshop.io.database.SearKeyDB;
import com.android.nearshop.io.database.ShopDB;
import com.android.nearshop.io.protocol.out.ShopDetail;
import com.android.nearshop.util.Constant;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;


/*******************************************************
 * @author: zhaohua
 * @version: 2011-12-19
 * @see 线性列表适配器，用于ListView,GridView,TableView。
 * @Copyright : copyrights reserved by personal 2007-2012
*******************************************************/
public class SearchHistoryAdapter extends ArrayListAdapter<ShopDetail> {
	
	 private OnClickListener mOnItemClick;
	
	public SearchHistoryAdapter(Context context) {
		super(context);
		
		mOnItemClick = new OnClickListener(){
    		@Override
    		public void onClick(View v) {
    			if(v.getTag() != null){
    				int shopId = (Integer) v.getTag();
	    			if(shopId > 0){
	    				Intent intent = new Intent();
	    				intent.setClass(mContext.getApplicationContext(), ShopDetailActivity.class);
	    				intent.putExtra(Constant.TAG_DETAIL_SHOPID, shopId);
	    				
	    				mContext.startActivity(intent);
	    			}
    			}
    		}
        };
	}

	//搜索数据库
	public void loadData(){
		List<ShopDetail> tmp = ShopDB.findBetch(null, "time DESC", Constant.HISTORY_SHOW_MAX);
		
		if(tmp == null || tmp.size() == 0){
			removeAll();
			return;
		}
		
		setList(tmp);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = inflater.inflate(R.layout.list_item_history, null);
			convertView.setOnClickListener(mOnItemClick);
		}
		((TextView)convertView.findViewById(R.id.tab_tx)).setText(mList.get(position).shopName);
		((TextView)convertView.findViewById(R.id.tab_addr)).setText(mList.get(position).shopAdd);
		convertView.setTag(mList.get(position).shopId);
		
		return convertView;
	}

}
